<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //将数组  ['关羽', '张飞', '程咬金','吕布']  转换为字符串
        var arr = ['关羽', '张飞', '程咬金', '吕布'];
        let result = "[";
        for (let index in arr) {
            // console.log(index, arr[index]);//遍历索引号和元素
            //result = result + "\'" + arr[index] + "\'" + ",";//输出为字符串数组
            if (index == arr.length - 1) {
                //console.log(arr[index]+"是最后一个元素");//吕布是最后一个元素
                result = result + "\'" + arr[index] + "\'"//['吕布']
            } else {
                result = result + "\'" + arr[index] + "\'" + ",";//['关羽','张飞','程咬金','吕布']
            }

        }
        result = result + "]";
        console.log(result);
    </script>
</body>

</html>